package com.zimi.service;

import com.zimi.mapper.WfDictionaryMapper;
import com.zimi.model.WfDictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:33
 * @Description:
 */
@Service
public class WfDictionaryService {

    @Autowired
    private WfDictionaryMapper wfDictionaryMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfDictionaryMapper.deleteByPrimaryKey(id);
    }

    public void deleteData(Integer id) {
        WfDictionary wfDictionary = wfDictionaryMapper.selectByPrimaryKey(id);
        if (wfDictionary != null && (wfDictionary.getParentId() == null || "".equals(wfDictionary.getParentId()))) {
            wfDictionaryMapper.deleteWfDictionaryByParentId(wfDictionary.getDictionaryId());
        } else {
            wfDictionaryMapper.deleteByPrimaryKey(id);
        }
    }


    public int insert(WfDictionary wfDictionary) {
        return wfDictionaryMapper.insert(wfDictionary);
    }


    public int insertSelective(WfDictionary wfDictionary) {
        if (wfDictionary.getParentId() != null && !"".equals(wfDictionary.getParentId())) {
            Integer serialNo = wfDictionaryMapper.selectMaxOrderNum(wfDictionary);
            if (serialNo == null) {
                serialNo = 1;
            } else {
                serialNo = serialNo + 1;
            }
            wfDictionary.setSerialNo(serialNo);
            return wfDictionaryMapper.insertSelective(wfDictionary);
        } else {
            return wfDictionaryMapper.insertSelective(wfDictionary);
        }
    }


    public WfDictionary selectByPrimaryKey(Integer id) {
        return wfDictionaryMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfDictionary wfDictionary) {
        return wfDictionaryMapper.updateByPrimaryKeySelective(wfDictionary);
    }


    public int updateByPrimaryKey(WfDictionary wfDictionary) {
        return wfDictionaryMapper.updateByPrimaryKey(wfDictionary);
    }

    public void updateData(WfDictionary wfDictionary) {
        if (wfDictionary != null && (wfDictionary.getParentId() == null || "".equals(wfDictionary.getParentId()))) {
            this.updateByPrimaryKeySelective(wfDictionary);
            WfDictionary wd = new WfDictionary();
            wd.setEnName(wfDictionary.getEnName());
            wd.setParentId(wfDictionary.getDictionaryId().toString());
            wfDictionaryMapper.updateWfDictionaryByParentId(wd);
        } else {
            wfDictionaryMapper.updateByPrimaryKeySelective(wfDictionary);
        }
    }


    public List<WfDictionary> selectAll(WfDictionary wfDictionary) {
        return wfDictionaryMapper.selectAll(wfDictionary);
    }

    public List<WfDictionary> selectWfDictionaryParent(WfDictionary wfDictionary) {
        return wfDictionaryMapper.selectWfDictionaryParent(wfDictionary);
    }

    public String selectMaxValue(WfDictionary wfDictionary) {
        return wfDictionaryMapper.selectMaxValue(wfDictionary);
    }

    public boolean insertinsertDuplicate(WfDictionary wfDictionary) {
        boolean flag = true;
        if (wfDictionary.getEnName() == null || "".equals(wfDictionary.getEnName())) {
            WfDictionary wd = new WfDictionary();
            wd.setDictionaryId(Integer.parseInt(wfDictionary.getParentId()));
            wd = selectByPrimaryKey(Integer.parseInt(wfDictionary.getParentId()));
            wfDictionary.setEnName(wd.getEnName());
        }
        if (wfDictionary.getValue() == null || "".equals(wfDictionary.getValue())) {
            WfDictionary wd = new WfDictionary();
            wd.setParentId(wfDictionary.getParentId());
            String val = selectMaxValue(wd);
            if (val == null || "".equals(val)) {
                val = "1";
            } else {
                val = Integer.toString(Integer.parseInt(val) + 1);
            }
            wfDictionary.setValue(val);
        }
        WfDictionary wfDictionary1 = new WfDictionary();
        wfDictionary1.setParentId(wfDictionary.getParentId());
        List<WfDictionary> wfDictionaryList = selectAll(wfDictionary1);
        if (wfDictionaryList.size() > 0) {
            for (WfDictionary dictionary : wfDictionaryList) {
                if (wfDictionary.getCnName().equals(dictionary.getCnName())) {
                    flag = false;
                }
                if (wfDictionary.getValue().equals(dictionary.getValue())) {
                    flag = false;
                }
            }
        }
        if (flag) {
            wfDictionaryMapper.insertSelective(wfDictionary);
        }
        return flag;
    }
}
